﻿using MyExe.Models;
using MyExeSetting;
using MyHttp;
using MyJson;
using MySQLite.DataHelp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.WebRequestMethods;

namespace MyExe.Help
{
    /// <summary>
    /// 接口地址
    /// </summary>
    public class HttpInterface
    {
        /// <summary>
        /// 心跳
        /// </summary>
        public const string HeartBeat = "api/File/HeartBeat";

        /// <summary>
        /// 文件信息上传
        /// </summary>
        public const string UploadFile = "api/File/UploadFile";

        /// <summary>
        /// 查询文件保存天数
        /// </summary>
        public const string SearchSaveFileDay = "api/File/SearchSaveFileDay";

        /// <summary>
        /// 删除文件
        /// </summary>
        public const string DeleteUploadFile = "api/File/DeleteUploadFile";
    }

    /// <summary>
    /// 通用返回封装
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class MyResponse<T>
    {
        public int Code { get; set; } = 0;
        public string Msg { get; set; }
        public T Data { get; set; }
        public string ResponseTime { get; set; } = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

    }

    public class HttpHelper
    {

        static MyHttpHelper myHttp = new MyHttpHelper();

        /// <summary>
        /// 心跳
        /// </summary>
        /// <returns></returns>
        public static bool SendHeartBeat()
        {
            try
            {
                string requestPath = MyExeSettingHelper.Instance.ServerUrl + HttpInterface.HeartBeat + $"?stationKey={MyExeSettingHelper.Instance.StationKey}";
                var result = myHttp.Request(requestPath, string.Empty, MyHttpType.Get);
                if (!string.IsNullOrEmpty(result))
                {
                    var myResult = MyJsonHelper.DeserializeJsonToObject<MyResponse<bool>>(result);
                    return myResult.Data;
                }
                else
                {
                    LogHelper.AddNetworkLogError("调用心跳接口失败");
                }
            }
            catch (Exception ex)
            {
                LogHelper.AddNetworkLogError($"调用心跳接口失败;{ex.Message}");
            }
            return false;
        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="fileInfo"></param>
        /// <returns></returns>
        public static bool UploadFile(RnFile fileInfo)
        {
            try
            {
                var strFile = MyJsonHelper.SerializeObjectToString(fileInfo);
                string requestPath = MyExeSettingHelper.Instance.ServerUrl + HttpInterface.UploadFile;
                var result = myHttp.Request(requestPath, strFile, MyHttpType.Post);
                if (!string.IsNullOrEmpty(result))
                {
                    var myResult = MyJsonHelper.DeserializeJsonToObject<MyResponse<bool>>(result);
                    return myResult.Data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.AddNetworkLogError($"文件上传失败;{ex.Message}");
            }
            return false;
        }

        /// <summary>
        /// 查询文件保存天数
        /// </summary>
        /// <param name="fileInfo"></param>
        /// <returns></returns>
        public static List<SaveFileDayDTO> SearchSaveFileDay()
        {
            try
            {
                string requestPath = MyExeSettingHelper.Instance.ServerUrl + HttpInterface.SearchSaveFileDay + $"?stationKey={MyExeSettingHelper.Instance.StationKey}";
                var result = myHttp.Request(requestPath, string.Empty, MyHttpType.Get);
                if (!string.IsNullOrEmpty(result))
                {
                    var myResult = MyJsonHelper.DeserializeJsonToObject<MyResponse<List<SaveFileDayDTO>>>(result);
                    return myResult.Data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.AddNetworkLogError($"查询文件保存天数失败;{ex.Message}");
            }
            return null;
        }


        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fileInfo"></param>
        /// <returns></returns>
        public static bool DeleteUploadFile(List<string> fileGuids)
        {
            try
            {
                var strFile = MyJsonHelper.SerializeObjectToString(fileGuids);
                string requestPath = MyExeSettingHelper.Instance.ServerUrl + HttpInterface.DeleteUploadFile;
                var result = myHttp.Request(requestPath, strFile, MyHttpType.Post);
                if (!string.IsNullOrEmpty(result))
                {
                    var myResult = MyJsonHelper.DeserializeJsonToObject<MyResponse<bool>>(result);
                    return myResult.Data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.AddNetworkLogError($"删除文件失败;{ex.Message}");
            }
            return false;
        }
    }
}
